package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;


    @Override
    public void add(Address address) {

        addressDao.add(address);

    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        Page<Address> page = addressDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Address> rows = page.getResult();
        return new PageResult(total, rows);
    }

    /**
     * 根据 id 查询 地址详情
     * @param id
     * @return
     */
    @Override
    public Address findById(Integer id) {
        return addressDao.findById(id);
    }

    /**
     *
     * 修改 数据 根据 ID
     * @param address
     */
    @Override
    public void edit(Address address) {
        addressDao.edit(address);
    }

    /**
     * 根据id 删除 地址信息
     * @param id
     */
    @Override
    public void delete(Integer id) {
        addressDao.delete(id);
    }

    @Override
    public List<Address> findAllByMap() {
        return addressDao.findAllByMap();
    }
}
